import 'package:chewie/chewie.dart' hide MaterialControls;
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'hi_video_controller.dart';

class VideoView extends StatefulWidget {
  final String url;
  final String cover;
  final double? width;
  final double? height;
  final bool looping;
  final bool autoPlay;
  final String vid;
  Widget? barrageUI;

  VideoView(
      {super.key,
      required this.url,
      required this.cover,
      required this.vid,
      this.looping = false,
      this.autoPlay = false,
      this.width,
      this.height,
      this.barrageUI});

  @override
  State<StatefulWidget> createState() {
    return _VideoViewState();
  }
}

class _VideoViewState extends State<VideoView> {
  late VideoPlayerController _videoPlayerController;
  late ChewieController _chewieController;

  @override
  void initState() {
    super.initState();
    _videoPlayerController =
        VideoPlayerController.networkUrl(Uri.parse(widget.url));
    _chewieController = ChewieController(
        videoPlayerController: _videoPlayerController,
        placeholder: FractionallySizedBox(
          widthFactor: 1,
          child: CachedNetworkImage(
            imageUrl: widget.cover,
            fit: BoxFit.cover,
            placeholder: (BuildContext context, String url) {
              return const CircularProgressIndicator(strokeWidth: 2);
            },
            errorWidget: (BuildContext context, String url, Object error) {
              return const Icon(Icons.error);
            },
          ),
        ),
        autoPlay: widget.autoPlay,
        looping: widget.looping,
        draggableProgressBar: false,
        showControlsOnInitialize: false,
        aspectRatio: 16.0 / 9.0,
        allowMuting: false,
        showControls: true,
        allowPlaybackSpeedChanging: false,
        customControls: MaterialControls(
          barrageUI: widget.barrageUI,
          showLoadingOnInitialize: false,
          showBigPlayIcon: false,
          bottomGradient: _blackLinearGradient(),
          overlayUI: _videoAppBar(),
        ));
  }

  @override
  void dispose() {
    _videoPlayerController.dispose();
    _chewieController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: widget.width,
      height: widget.height,
      child: Chewie(controller: _chewieController),
    );
  }

  ///黑色线性渐变
  _blackLinearGradient({bool fromTop = false}) {
    return LinearGradient(
        begin: fromTop ? Alignment.topCenter : Alignment.bottomCenter,
        end: fromTop ? Alignment.bottomCenter : Alignment.topCenter,
        colors: const [
          Colors.black54,
          Colors.black45,
          Colors.black38,
          Colors.black26,
          Colors.black12,
          Colors.transparent
        ]);
  }

  ///视频详情页appBar
  _videoAppBar() {
    return Container(
      padding: const EdgeInsets.only(right: 8),
      decoration: BoxDecoration(gradient: _blackLinearGradient(fromTop: true)),
      child: const Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          BackButton(
            color: Colors.white,
          ),
          Row(
            children: [
              Icon(Icons.live_tv_rounded, color: Colors.white, size: 20),
              Padding(
                padding: EdgeInsets.only(left: 12),
                child: Icon(Icons.more_vert_rounded,
                    color: Colors.white, size: 20),
              )
            ],
          )
        ],
      ),
    );
  }
}
